package com.pls.service;

import java.util.ArrayList;

import com.pls.handlers.DeleteEmployeeSeatHandler;
import com.pls.handlers.GetProjectsHandler;
import com.pls.handlers.GetQuadrantHandler;
import com.pls.handlers.SearchEmployeeHandler;
import com.pls.handlers.UpdateEmployeeSeatHandler;
import com.pls.helpers.ExportToFile;
import com.plsContracts.dataContracts.DeleteEmployeeSeatRequest;
import com.plsContracts.dataContracts.DeleteEmployeeSeatResponse;
import com.plsContracts.dataContracts.GetProjectsRequest;
import com.plsContracts.dataContracts.GetProjectsResponse;
import com.plsContracts.dataContracts.GetQuadrantRequest;
import com.plsContracts.dataContracts.GetQuadrantResponse;
import com.plsContracts.dataContracts.SearchEmployeeRequest;
import com.plsContracts.dataContracts.SearchEmployeeResponse;
import com.plsContracts.dataContracts.UpdateEmployeeSeatRequest;
import com.plsContracts.dataContracts.UpdateEmployeeSeatResponse;
import com.plsDTO.dataObjects.EmployeeWorkstation;
import com.plsDTO.dataObjects.Project;
import com.plsDTO.dataObjects.Seat;
import com.plsDTO.dataObjects.WorkstationMap;
import com.plsDTO.validation.ValidationError;

public class PeopleLocatorSystem {

	public SearchEmployeeResponse searchEmployee(SearchEmployeeRequest request) {

		SearchEmployeeHandler handler = new SearchEmployeeHandler(
				SearchEmployeeResponse.class);
		SearchEmployeeResponse empResponse = handler.Execute(request);
		
		//PROJECT
		GetProjectsRequest projRequest = new GetProjectsRequest();
		projRequest.setEmployeeId(request.getEmployeeId());
		GetProjectsResponse projResponse = new GetProjectsResponse();
		
		GetProjectsHandler projHandler = new GetProjectsHandler(GetProjectsResponse.class);
		projResponse = projHandler.Execute(projRequest);
		ArrayList<Project> projects = new ArrayList<Project>();
		projects = projResponse.getProjectList();
		empResponse.getEmployeeWorkstation().setProjectList(projects);
		
		//Get Quadrant map
		GetQuadrantHandler quandrantHandler = new GetQuadrantHandler(
				GetQuadrantResponse.class);
		GetQuadrantRequest quandrantRequest = new GetQuadrantRequest();
		
		//Get quadrant code from seat code
		String seatCode = empResponse.getEmployeeWorkstation().getSeatDetails().getSeatCode();
		String quadrant = empResponse.getEmployeeWorkstation().getSeatDetails().getQuadrant();
		
		//set the code to pattern
		empResponse.getEmployeeWorkstation().getSeatDetails().setQuadrant(quadrant);
		quadrant = seatCode.substring(0, seatCode.indexOf("F") + 1);
		
		//Get quadrant details
		quandrantRequest.setQuadrant(quadrant);
		GetQuadrantResponse quadrantResponse = quandrantHandler.Execute(quandrantRequest);
		
		//Employee workstation
		empResponse.setSeatMap(quadrantResponse.getEmployee());
		ArrayList<EmployeeWorkstation> workstationMap = quadrantResponse.getEmployee(); 
		
		//Call text printer
		boolean success = ExportToFile.createTextFile(empResponse.getEmployeeWorkstation(), workstationMap);
		ExportToFile.createExcelFile(empResponse.getEmployeeWorkstation(), workstationMap);
		
		return empResponse;

	}

	public UpdateEmployeeSeatResponse updateEmployeeSeat(
			UpdateEmployeeSeatRequest request) {

		UpdateEmployeeSeatHandler handler = new UpdateEmployeeSeatHandler(
				UpdateEmployeeSeatResponse.class);
		UpdateEmployeeSeatResponse response = handler.Execute(request);

		return response;

	}

	public DeleteEmployeeSeatResponse deleteEmployeeSeat(
			DeleteEmployeeSeatRequest request) {

		DeleteEmployeeSeatHandler handler = new DeleteEmployeeSeatHandler(
				DeleteEmployeeSeatResponse.class);
		DeleteEmployeeSeatResponse response = handler.Execute(request);

		return response;

	}

	public GetProjectsResponse getProjectsByEmployeeId(
			GetProjectsRequest request) {

		GetProjectsHandler handler = new GetProjectsHandler(
				GetProjectsResponse.class);
		GetProjectsResponse response = handler.Execute(request);

		return response;

	}
	
	private GetQuadrantResponse getQuadrantBySeatCode(
			GetQuadrantRequest request) {

		GetQuadrantHandler handler = new GetQuadrantHandler(
				GetQuadrantResponse.class);
		GetQuadrantResponse response = handler.Execute(request);

		return response;

	}

}
